<template>
  <div class="reg">
    <el-form :model="reg.ruleForm" status-icon :rules="reg.rules" ref="ruleForm" label-width="0" class="demo-ruleForm">
      <el-form-item  prop="account">
        <el-input v-model="reg.ruleForm.account" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item  prop="pass">
        <el-input type="password" v-model="reg.ruleForm.pass" placeholder="密码" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item  prop="checkPass">
        <el-input type="password" v-model="reg.ruleForm.checkPass" placeholder="确认密码" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width:100%" type="primary" @click="submitForm('ruleForm')">立即注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script> 
  export default {
    data () {
      var checkUser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.reg.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.reg.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        reg:{
          ruleForm: {
            pass: '',
            checkPass: '',
            account: ''
          },
          rules: {
            pass: [
              { validator: validatePass, trigger: 'blur' }
            ],
            checkPass: [
              { validator: validatePass2, trigger: 'blur' }
            ],
            account: [
              { validator: checkUser, trigger: 'blur' }
            ]
          }
        }
      }
    },
    mounted () {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = {
              account: this.reg.ruleForm.account,
              password: this.reg.ruleForm.pass,
              status: 1
            };
            this.$api.register(params)
             .then(res => {
              if(res.code == 200){
                this.$message({
                  message: '注册成功',
                  type: 'success'
                });
                this.reload()
              }
             })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    },
    components: {
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang='less' scoped>

@import './register.less';

</style>